package org.bt.common.validator.number;

import org.bt.common.validator.aspect.Min;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * AbstractMinValidator
 * <p>
 * Created on 2020/1/9
 *
 * @author tongw
 */
public abstract class AbstractMinValidator<T> implements ConstraintValidator<Min, T> {

    protected long minValue;

    @Override
    public void initialize(Min maxValue) {
        this.minValue = maxValue.value();
    }

    @Override
    public boolean isValid(T value, ConstraintValidatorContext constraintValidatorContext) {
        // null values are valid
        if (value == null) {
            return true;
        }

        return compare(value) >= 0;
    }

    protected abstract int compare(T number);
}
